/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.core.instrument;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.open.cobertura.core.provider.ICouerturaProbeInsertProvider;

public class CoberturaMethodInsrumenter extends CoberturaMethodProbeVisitor{
    private ICouerturaProbeInsertProvider coberturaProbeIndexProvider;
    private long classId;
    private String className;
    protected CoberturaMethodInsrumenter(final MethodVisitor mv,final long classId,final String className,ICouerturaProbeInsertProvider coberturaProbeIndexProvider) {
        super(mv);
        this.coberturaProbeIndexProvider=coberturaProbeIndexProvider;
        this.classId=classId;
        this.className=className;
    }

    @Override
    public void visitLabel(Label label) {
        super.visitLabel(label);
    }


    @Override
    public void insertJumpProbe(final int opcode,int index, final Label label,IFrame frame) {
        if(opcode== Opcodes.GOTO) {
            //这里应该还要算一下frame
            coberturaProbeIndexProvider.insertProbeBefore(mv, className, index);
            mv.visitJumpInsn(Opcodes.GOTO,label);
        }else{
            final Label intermediate = new Label();
            //插入反转的代码
            mv.visitJumpInsn(covertJumpInverted(opcode), intermediate);
            coberturaProbeIndexProvider.insertProbeAfter(mv, className,index);
            //跳转到原来的标签
            mv.visitJumpInsn(Opcodes.GOTO, label);
            mv.visitLabel(intermediate);
//            frame.accept(mv);
        }
    }

    @Override
    public void insertProbe(final int opcode,int index) {
        coberturaProbeIndexProvider.insertProbeBefore(mv,className,index);
        insertWriteCoberturaProbeData(classId,className);
        mv.visitInsn(opcode);
    }


    private void insertWriteCoberturaProbeData(long classId, String className) {
        coberturaProbeIndexProvider.insertWriteProbeData(mv,classId,className);
    }

    @Override
    public void insertProbe(int index) {
        coberturaProbeIndexProvider.insertProbeBefore(mv,className,index);
    }

    /**
     * 跳转指令反转，为了让else指令能正常的监测到
     * @param opcode
     * @return
     */
    private int covertJumpInverted(final int opcode) {
            if(opcode==Opcodes.IFEQ) {
                return IFNE();
            }else if(opcode==Opcodes.IFNE) {
                return IFEQ();
            }
            else if(opcode==Opcodes.IFLT) {
                return IFGE();
            }
            else if(opcode==Opcodes.IFGE) {
                return IFLT();
            }
            else if(opcode==Opcodes.IFGT) {
                return IFLE();
            }
            else if(opcode==Opcodes.IFLE) {
                return IFGT();
            }
            else if(opcode==Opcodes.IF_ICMPEQ) {
                return IF_ICMPNE();
            }
            else if(opcode==Opcodes.IF_ICMPNE) {
                return IF_ICMPEQ();
            }
            else if(opcode==Opcodes.IF_ICMPLT) {
                return IF_ICMPGE();
            }
            else if(opcode==Opcodes.IF_ICMPGE) {
                return IF_ICMPLT();
            }
            else if(opcode==Opcodes.IF_ICMPGT) {
                return IF_ICMPLE();
            }
            else if(opcode==Opcodes.IF_ICMPLE) {
                return IF_ICMPGT();
            }
            else if(opcode==Opcodes.IF_ACMPEQ) {
                return IF_ACMPNE();
            }
            else if(opcode==Opcodes.IF_ACMPNE) {
                return IF_ACMPEQ();
            }
            else if(opcode==Opcodes.IFNULL) {
                return IFNONNULL();
            }
            else if(opcode==Opcodes.IFNONNULL) {
                return IFNULL();
            }
            throw new IllegalArgumentException();
    }


}
